<?php

namespace Api\Controller;

//use Yansongda\Pay\Pay;

class ApiuserController extends UserBaseController{
    //会员账户接口
    public function userinfo(){
        if(!I('post.signature'))
            $this->jsonReturn ($this->getMsg('success', '403', "参数错误"));
        $user=$this->getUserForSign(I('post.signature'));
        $result=M('recom_user')->where(array('user_id'=>$user['id']))->find();
        if(!$result)
            $this->jsonReturn ('fail',403,"参数不正确");
        else 
            $this->jsonReturn ('success',200,$result);
    }
    //我的好友接口
    public function myfriend(){
        if(!I('post.signature'))
            $this->jsonReturn ($this->getMsg('fail', '403', "参数错误"));
        $user=$this->getUserForSign(I('post.signature'));
        $count=M('recom')->where(array('user_id'=>$user['id']))->count();//好友人数
        $count_cost=M()->table('8diantao_user a,8diantao_recom_list b,8diantao_user c')
                ->field('sum(account) as sum_cost')
                ->where('a.id=b.vip_id and b.next_id=c.id and typeid=1')->find();
        $count_cost_sum=$count_cost['sum_cost']?:0;
        $data['sum_cost']=$count_cost_sum?:0;
        $data['invite']=$count;
        $result=M()->table('8diantao_user a,8diantao_recom_list b,8diantao_user c')
                ->field('a.username ,a.mobile,a.isweixin,a.wxnickname,sum(account) as sum_cost,a.last_time as times,a.id ')
                ->where('a.id=b.vip_id and b.next_id=c.id and typeid=1')->group('c.username')->select();
        $data['record']=$result;
        if($result)
            $this->jsonReturn($this->getMsg('success', '200', $data));
        else
            $this->jsonReturn($this->getMsg('success', '200', "暂无记录"));
    }
    //我的收入
    public function costrm(){
        if(!I('post.signature'))
            $this->jsonReturn ($this->getMsg('fail', '403', "参数错误"));
        $user=$this->getUserForSign(I('post.signature'));
        $result=M()->table('8diantao_user a,8diantao_recom_list b,8diantao_user c')
                ->field('a.username ,a.mobile,a.isweixin,a.wxnickname,account,create_at as times,order_id ,type_alias as class')
    ->where('a.id=b.vip_id and b.next_id=c.id and typeid=1 and b.vip_id={$user["id"]}')->select();
        if($result)
            $this->jsonReturn($this->getMsg('success', '200', $result));
        else
            $this->jsonReturn($this->getMsg('success', '200', "暂无记录"));
    }
    public function inviterm(){//邀请
        if(!I('post.signature'))
            $this->jsonReturn ($this->getMsg('fail', '403', "参数错误"));
        $user=$this->getUserForSign(I('post.signature'));
        $result=M()->table('8diantao_user a,8diantao_recom_list b,8diantao_user c')
                ->field('a.username ,a.mobile,a.isweixin,a.wxnickname,account,create_at as times,order_id ,type_alias as class')
                ->where('a.id=b.vip_id and b.next_id=c.id and typeid=0 and b.vip_id={$user["id"]}')->select();
        if($result)
            $this->jsonReturn($this->getMsg('success', '200', $result));
        else
            $this->jsonReturn($this->getMsg('success', '200', "暂无记录"));
    }
    public function qd_fx($user){    //新手红包判断流程
        $result=M('recom_hongbao')->where(array('user_id'=>$user['id']))->find();
        if($result['fenxiang']>5&&$result['qiandao']>=5&&$result['hb1']==0){
            M('recom_hongbao')->where(array('id'=>$result['id']))->save(array('hb1'=>1));
            M('recom_user')->where(array('user_id'=>$result['user_id']))->setInc('tx_money',3);
            M('recom_account_log')->add(array('user_id'=>$result['user_id'],'account'=>3,'op'=>'签5分5','create_at'=>time()));
        }elseif($result['fenxiang']>15&&$result['qiandao']>=10&&$result['hb2']==0){
            M('recom_hongbao')->where(array('id'=>$result['id']))->save(array('hb2'=>1));
            M('recom_user')->where(array('user_id'=>$result['user_id']))->setInc('tx_money',5);
            M('recom_account_log')->add(array('user_id'=>$result['user_id'],'account'=>5,'op'=>'签10分15','create_at'=>time()));          
        }elseif($result['qiandao']>=20&&$result['gouwu']>=1&&$result['hb3']==0){//12
            M('recom_hongbao')->where(array('id'=>$result['id']))->save(array('hb3'=>1));
            M('recom_user')->where(array('user_id'=>$result['user_id']))->setInc('tx_money',12);
            M('recom_account_log')->add(array('user_id'=>$result['user_id'],'account'=>12,'op'=>'签20购1','create_at'=>time()));
        }elseif($result['qiandao']>=30&&$result['gouwu']>=3&&$result['hb4']==0){//20
            M('recom_hongbao')->where(array('id'=>$result['id']))->save(array('hb4'=>1));
            M('recom_user')->where(array('user_id'=>$result['user_id']))->setInc('tx_money',20);
            M('recom_account_log')->add(array('user_id'=>$result['user_id'],'account'=>20,'op'=>'签30购3','create_at'=>time()));
        }elseif($result['qiandao']>=60&&$result['gouwu']>=6&&$result['hb5']==0){//35
            M('recom_hongbao')->where(array('id'=>$result['id']))->save(array('hb5'=>1));
            M('recom_user')->where(array('user_id'=>$result['user_id']))->setInc('tx_money',35);
            M('recom_account_log')->add(array('user_id'=>$result['user_id'],'account'=>35,'op'=>'签60购6','create_at'=>time()));
        }elseif($result['qiandao']>=90&&$result['gouwu']>=10&&$result['hb6']==0){//50
            M('recom_hongbao')->where(array('id'=>$result['id']))->save(array('hb6'=>1));
            M('recom_user')->where(array('user_id'=>$result['user_id']))->setInc('tx_money',50);
            M('recom_account_log')->add(array('user_id'=>$result['user_id'],'account'=>50,'op'=>'签90购10','create_at'=>time()));
        }elseif($result['qiandao']>180&&$result['gouwu']>=20&&$result['hb7']==0){//65
            M('recom_hongbao')->where(array('id'=>$result['id']))->save(array('hb7'=>1));
            M('recom_user')->where(array('user_id'=>$result['user_id']))->setInc('tx_money',65);
            M('recom_account_log')->add(array('user_id'=>$result['user_id'],'account'=>65,'op'=>'签180购20','create_at'=>time()));
        }
        return $result;
    }
    public function fenxiang(){//分享+1
        if(!I('post.signature'))
            $this->jsonReturn ($this->getMsg('fail', '403', "参数错误"));
        $user=$this->getUserForSign(I('post.signature'));
        $result=  $this->qd_fx($user);
        M('recom_hongbao')->where(array('id'=>$result['id']))->save(array('fenxiang'=>$result['fenxiang']+1));

    }
    public function qiandao(){//签到+1
        if(!I('post.signature'))
            $this->jsonReturn ($this->getMsg('fail', '403', "参数错误"));
        $user=$this->getUserForSign(I('post.signature'));
        $result=$this->qd_fx($user);
        $rate=M('recom_userrate')->where(array('user_id'=>$user['id']))->find();
        if($rate){
            $rate_add=($rate['rate']+60)<100?$$rate['rate']+60:100;
            $invite_add=60;
            M('recom_userrate')->where(array('user_id'=>$user['id']))->save(array('rate'=>$rate_add,'login_add'=>$invite_add));
        }
        M('recom_hongbao')->where(array('id'=>$result['id']))->save(array('qiandao'=>$result['qiandao']+1));
    }
    public function xiaofei(){//消费+1
        if(!I('post.signature'))
            $this->jsonReturn ($this->getMsg('fail', '403', "参数错误"));
        $user=$this->getUserForSign(I('post.signature'));
        $result=$this->qd_fx($user); 
        M('recom_hongbao')->where(array('id'=>$result['id']))->save(array('gouwu'=>$result['gouwu']+1));
    }
}